#include "client.h"

int isValidCommand(const char* cmd){
    if(strcmp("ls",cmd)==0){
        return 1;
    }
    else if(strcmp("cd",cmd)==0){
        return 1;
    }
    else if(strcmp("pwd",cmd)==0){
        return 1;
    }
    else if(strcmp("mkdir",cmd)==0){
        return 1;
    }
    else if(strcmp("rmdir",cmd)==0){
        return 1;
    }
    else if(strcmp("puts",cmd)==0){
        return 1;
    }
    else if(strcmp("gets",cmd)==0){
        return 1;
    }
    else if(strcmp("remove",cmd)==0){
        return 1;
    }
    return 0;
}

int getCommandType(const char *cmd,commandPacket_t* pcommandPacket){
    if(strcmp("ls",cmd)==0){
        pcommandPacket->cmd=LSCOMMAND;
    }
    else if(strcmp("cd",cmd)==0){
        pcommandPacket->cmd=CDCOMMAND;
    }
    else if(strcmp("pwd",cmd)==0){
        pcommandPacket->cmd=PWDCOMMAND;
    }
    else if(strcmp("mkdir",cmd)==0){
        pcommandPacket->cmd=MKDIRCOMMAND;
    }
    else if(strcmp("rmdir",cmd)==0){
        pcommandPacket->cmd=RMDIRCOMMAND;
    }
    else if(strcmp("puts",cmd)==0){
        pcommandPacket->cmd=PUTSCOMMAND;
    }
    else if(strcmp("gets",cmd)==0){
        pcommandPacket->cmd=GETSCOMMAND;
    }
    else if(strcmp("remove",cmd)==0){
        pcommandPacket->cmd=REMOVECOMMAND;
    }
    return 0;

}

int recvn(int sockfd,void *buf,int length){
    ssize_t cur_count=0;
    char *p=(char *)buf;
    while(cur_count<length){
        ssize_t sret=recv(sockfd,p+cur_count,length-cur_count,0);
        cur_count+=sret;
    }
    return 0;
}

int setCommandPacket(commandPacket_t* pcommandPacket,const char *cmd,const char *arg){
    getCommandType(cmd,pcommandPacket);                                              
    if(arg==NULL)
    {
        pcommandPacket->length=0;
    }
    else
    {
        pcommandPacket->length=strlen(arg);
    }
    if(arg!=NULL)
        strcpy(pcommandPacket->argument,arg);
    else
    {
        strcpy(pcommandPacket->argument,".");
    }
    return 0;
}
//  上传文件
int putsFunc(int sockfd, char *filename) {
    int file_len=strlen(filename);
    send(sockfd,&file_len,sizeof(int),MSG_NOSIGNAL);
    send(sockfd,filename,file_len,MSG_NOSIGNAL);
    off_t filesize;
    recv(sockfd,&filesize,sizeof(off_t),MSG_WAITALL);
    if(filesize==-1)
    {
        printf("no such file\n");
        return 0;
    }
    else
    {
        printf("filesize=%ld\n",filesize);
    }
    int fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,0666);
    char buf[4096]={0};
    int cursize=0;
    while(cursize<filesize)
    {
        int recvsize=filesize-cursize;
        if(recvsize>=4096)
            recvsize=4096;
        int ret=recv(sockfd,buf,recvsize,MSG_WAITALL);
        write(fd,buf,ret);
        cursize+=recvsize;
    }
    return 0;
}
// 下载文件
int getsFunc(int netfd, char *filename) {
    int file_len;
    recv(netfd,&file_len,sizeof(file_len),MSG_WAITALL);
    recv(netfd,filename,file_len,MSG_WAITALL);
    int access_ret = access(filename, F_OK);
    int fd;
    off_t filesize;
    if (access_ret == -1)
    {
        //发送-1给对面i
        off_t a=(off_t)access_ret;
        send(netfd,&a,sizeof(off_t),MSG_NOSIGNAL);
        printf("no such file\n");
        return 0;
    }
    else
    {
        fd=open(filename,O_RDWR);
        struct stat statbuf;
        fd=open(filename,O_RDWR);
        fstat(fd,&statbuf);
        filesize=statbuf.st_size;
        send(netfd,&filesize,sizeof(off_t),MSG_NOSIGNAL);
    }
    sendfile(netfd,fd,NULL,filesize);
    return 0;
    
}
